package cheme.ProcessEditor.Table;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ToolbarLayout;

import cheme.ProcessEditor.Parts.Streams.Tag.TableRowData;

public class TableLayout extends ToolbarLayout implements ITableLayout
{
	int spacing = 1;
	TableRowData masterRowData = null;
	Figure figure = null;
		
	List<ITableElementLayout> childLayouts = new ArrayList<ITableElementLayout>();
			
	public TableLayout(Figure figure)
	{
		spacing = 1;
		this.figure = figure;
	}

	public void updateMaxRowData(ITableElementLayout source)
	{
//		System.out.println("TableLayout.updateMaxRowData -- start");
		
		TableRowData newRowData = calculateMaxRowData(source);
		
		if(null == getRowData() || !getRowData().equals(newRowData))
		{
			setRowData(newRowData);
			
			/*
			 * It is important here to invalidate the whole subtree as the newly adjusted row 
			 * data may not apply to this layout's direct descendents.
			 */
			figure.invalidateTree();
			figure.getUpdateManager().performValidation();
		}

//		System.out.println("TableLayout . updateMaxRowData -- end");
	}
	
	boolean calculating = false;
	
	TableRowData calculateMaxRowData(ITableElementLayout source)
	{
//		System.out.println("TableLayout.calculateMaxRowData -- start");
		
		TableRowData masterData = null;
		if(!calculating)
		{
			calculating = true;
		
			/*
			 * Poll the children layouts to find the optimal size for each column
			 */
			
			TableRowData currentData = null;
			ColumnSpan columnSpan = null;
			
			masterData = new TableRowData();
			
			/*
			 * Get the initial max column widths across all rows
			 */
			
			for(ITableElementLayout row : childLayouts)
			{
				/*
				 * Restore the row's actual or ideal column widths for a fresh comparison
				 */
				
				currentData = row.getActualRowData();
				
//				if(source == row)
//				{
//					currentData = row.getAdjustedRowData();
////					System.out.println("Looking at newly adjusted data : " + currentData);
//				}
//				else
//				{
//					currentData = row.getActualRowData();
////					System.out.println("Looking at initial (actual): " + currentData);
//				}
				
				if(null != currentData)
				{
					for(int i = 0; i < currentData.spans.size(); i++)
					{
						if(i >= masterData.spans.size())
						{
							columnSpan = new ColumnSpan(0);
							masterData.spans.add(columnSpan);
						}
						else
						{
							columnSpan = masterData.spans.get(i);
						}
						
						columnSpan.width = Math.max(columnSpan.width, currentData.spans.get(i).width);
					}
				}
			}
			
			calculating = false;
		}
		
//		System.out.println("TableLayout.calculateMaxRowData -- end");
		
		return masterData;
	}

	public TableRowData getRowData()
	{
		return masterRowData;
	}
	
	public void setRowData(TableRowData rowData)
	{
		masterRowData = rowData;
	}
	
	public ITableElementLayout getNewLayoutManager(IFigure figure)
	{
		ITableElementLayout layout = null;
		
		if(figure instanceof ISubTable)
		{
			layout = (ITableElementLayout)figure.getLayoutManager(); //new SubTableLayout(this);
			layout.setParent(this);
		}
		else
		{
			layout = new TableRowLayout(this);
		}
		
		return layout;
	}
	
	public void add(IFigure figure)
	{
		add(figure, -1);
	}
	
	public void add(IFigure figure, int index)
	{
		ITableElementLayout layout = getNewLayoutManager(figure);
		
		figure.setLayoutManager(layout);
		childLayouts.add(layout);
		
		super.setConstraint(figure, layout);
	}
	
	@Override
	public void remove(IFigure child)
	{
		childLayouts.remove(child.getLayoutManager());
		super.remove(child);
	}
}
